package com.pengtu.service.human;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;


import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.dao.human.LeaveDao;
import com.pengtu.dao.human.OvertimeDao;
import com.pengtu.entity.human.Leave;
import com.pengtu.entity.human.Overtime;
import com.pengtu.service.BaseManager;
import com.pengtu.service.app.AccountManager;
import com.pengtu.utils.DateUtils;
import com.pengtu.utils.ExcelHelper;


@Transactional
@Component
public class LeaveOvertimeManager extends BaseManager{

	private LeaveDao leaveDao;
	private OvertimeDao overtimeDao;
	private AccountManager accountManager;
	@Autowired
	public void setAccountManager(AccountManager accountManager) {
		this.accountManager = accountManager;
	}

	@Autowired
	public void setLeaveDao(LeaveDao leaveDao) {
		this.leaveDao = leaveDao;
	}

	@Autowired
	public void setOvertimeDao(OvertimeDao overtimeDao) {
		this.overtimeDao = overtimeDao;
	}

	// 操作对象leave
	@Transactional(readOnly = true)
	public Leave getLeave(String id) {
		return leaveDao.get(id);
	}

	@Transactional(readOnly = true)
	public List<Leave> getAllLeave() {
		return leaveDao.getAll();
	}

	public void saveLeave(Leave leave) {
		leaveDao.save(leave);
	}

	public void deleteLeave(String id) {
		leaveDao.delete(id);
	}

	/**
	 * 按属性过滤条件列表查找对象列表
	 * 
	 * @param filter
	 * @return
	 */
	@Transactional(readOnly = true)
	public List<Leave> findLeave(QueryFilter filter) {

		return leaveDao.find(filter);
	}

	/**
	 * findPageLeave
	 * 模糊查询并按分页获取数据
	 * @param page
	 * @param queryfilter
	 * @return
	 */
	@Transactional(readOnly=true)
	public Page<Leave> findPageLeave(final Page<Leave> page,final QueryFilter queryfilter){
		return leaveDao.findPage(page, queryfilter);
	}
	
	/**
	 * findPropertyLeave
	 * 根据属性名称和值获取单条数据信息
	 * @param propertyName
	 * @param value
	 * @return
	 */
	@Transactional(readOnly=true)
	public Leave findPropertyLeave(String propertyName,String value){
		if(propertyName.equalsIgnoreCase("id")){
			return leaveDao.get(value);
		}
		return leaveDao.findUniqueBy(propertyName, value);
	}


	// 操作对象overtime
	@Transactional(readOnly = true)
	public Overtime getOvertime(String id) {
		return overtimeDao.get(id);
	}

	@Transactional(readOnly = true)
	public List<Overtime> getAllOvertime() {
		return overtimeDao.getAll();
	}

	public void saveOvertime(Overtime overtime) {
		overtimeDao.save(overtime);
	}

	public void deleteOvertime(String id) {
		overtimeDao.delete(id);
	}

	/**
	 * 按属性过滤条件列表查找对象列表
	 * 
	 * @param filter
	 * @return
	 */
	@Transactional(readOnly = true)
	public List<Overtime> findOvertime(QueryFilter filter) {

		return overtimeDao.find(filter);
	}
	
	/**
	 * 根据属性名称和值获取单条数据信息
	 * @param propertyName
	 * @param value
	 * @return
	 */
	@Transactional(readOnly=true)
	public Overtime findPropertyOvertime(String propertyName,String value){
		if(propertyName.equalsIgnoreCase("id")){
			return overtimeDao.get(value);
		}
		return overtimeDao.findUniqueBy(propertyName, value);
	}
	/**
	 * 根据属性过滤条件列表查找对象列表
	 * @param page
	 * @param queryfilter
	 * @return
	 */
	@Transactional(readOnly=true)
	public Page<Overtime> findPageOvertime(final Page<Overtime> page,final QueryFilter queryfilter){
		
		return overtimeDao.findPage(page, queryfilter);
	}
//----------------------------------------------------------------
	/**
	 * 适用:查询请假加班两张表的数据
	 * @param filter
	 * @return
	 * @throws Exception
	 */
	public Map<String, Map<String, Object>> getleaveOvertimeMap(
			QueryFilter filter) {
		Map<String, Map<String, Object>> lotMap = new HashMap<String, Map<String, Object>>();
		List<Leave> leaveList = this.findLeave(filter);
		for (Leave leave : leaveList) {
			String i = leave.getUserid(); // 根据用户来判断
			Map<String, Object> areMap = lotMap.get(i);
			if (areMap == null) {
				areMap = new HashMap<String, Object>();
			}
			Object deptid = leave.getDeptid();
			Object userid = leave.getUserid();
			Integer ldate = DateUtils.betweenDays(leave.getEnddate(),
					leave.getStartdate());
			Object amount = areMap.get("ldate");
			if (amount != null) {
				ldate = ldate + (Integer) amount;
			}
			areMap.put("deptid", deptid);
			areMap.put("userid", userid);
			areMap.put("ldate", ldate);
			lotMap.put(i, areMap);
		}
		List<Overtime> overtimeList = this.findOvertime(filter);
		for (Overtime ot : overtimeList) {
			String j = ot.getUserid();
			Map<String, Object> otMap = lotMap.get(j);
			if (otMap == null) {
				otMap = new HashMap<String, Object>();
			}
			Object deptid = ot.getDeptid();
			Object userid = ot.getUserid();
			Integer odate = DateUtils.betweenDays(ot.getEnddate(),
					ot.getStartdate());// 一次
			Object total = otMap.get("odate");
			if (total != null) {
				odate = odate + (Integer) total;
			}
			otMap.put("deptid", deptid);
			otMap.put("userid", userid);
			otMap.put("odate", odate);
			lotMap.put(j, otMap);
		}
		return lotMap;
	}
	
	/**
	 * 适用:提取数据
	 * @param rootPath
	 * @param filter
	 * @return
	 * @throws Exception
	 */
   public String exportleaveOvertime(String rootPath,QueryFilter filter) throws Exception{
	   Map<String, Map<String, Object>> lotMap  = this.getleaveOvertimeMap(filter);
	   ExcelHelper helper = ExcelHelper.getInstance();
	   String excleName = "temp/"+System.currentTimeMillis()+".xls";
	   WritableWorkbook wwb = helper.createWritableWorkbook(rootPath+excleName,
			   rootPath+"/resources/leaveovertime.xls");
		WritableSheet sheet = wwb.getSheets()[0];
		Set<Map.Entry<String, Map<String, Object>>> leaveOverTimes = lotMap
				.entrySet();
		int r = 3;
		for (Map.Entry<String, Map<String, Object>> entry : leaveOverTimes) {
			Map<String, Object> leaveOverTime = entry.getValue();
			sheet = helper.addCell(sheet, 0, r, r-2);
			sheet = helper.addCell(sheet, 1, r, leaveOverTime.get("userid"));
			String deptid=(String) leaveOverTime.get("deptid");
			String depName = accountManager.getDepartment(deptid).getDepName();		
			sheet=helper.addCell(sheet, 2, r, depName);
			sheet = helper.addCell(sheet, 3, r, leaveOverTime.get("ldate"));
			sheet = helper.addCell(sheet, 4, r, leaveOverTime.get("odate"));
			Integer odate  = (Integer)leaveOverTime.get("odate");
			Integer ldate=(Integer) leaveOverTime.get("ldate");
			Integer sumDate;
			if(ldate!=null && odate!=null){
				 sumDate=odate-ldate;
				sheet=helper.addCell(sheet, 5, r, sumDate);
			}else if(ldate!=null && odate==null){
				sumDate=0-ldate;
				sheet=helper.addCell(sheet, 5, r, sumDate);
			}else if(ldate==null && odate!=null){
				sumDate=odate-0;
				sheet=helper.addCell(sheet, 5, r, sumDate);
			}
			r++;
		}
	   wwb.write();
	   wwb.close();
	  return excleName;
	   
   }

	
	}


